<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('商品分类')">
                <a-tree-select
                  allowClear
                  v-model="queryParam.parentKindId"
                  :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeData"
                  :search-placeholder="$t('请选择')"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('f_商品信息')">
                <a-input :placeholder="$t('请输入商品编码/名称/条码')" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="8" :lg="12" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{ $t('今日') }} </a-menu-item>
                  <a-menu-item key="2"> {{ $t('近三天') }} </a-menu-item>
                  <a-menu-item key="3"> {{ $t('近七天') }} </a-menu-item>
                  <a-menu-item key="4"> {{ $t('近一月') }} </a-menu-item>
                  <a-menu-item key="5"> {{ $t('近三月') }} </a-menu-item>
                  <a-menu-item key="6"> {{ $t('近半年') }} </a-menu-item>
                  <a-menu-item key="7"> {{ $t('本年') }} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px">
                  {{ $t('快捷查询') }} <a-icon type="down"
                /></a-button>
              </a-dropdown>
              <a-button type="primary" style="margin-left: 8px" @click="searchReset">{{ $t('重置') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
              <a-button type="primary" :loading="exloading" style="margin-left: 8px" @click="handleExportXls($t('礼品回收汇总'))" v-has="'BisGiftReclaimRecordController:export'">{{
                $t('导出')
              }}</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="kindId"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'GiftRecyclingSummary',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '礼品回收汇总页面',
      // 表头
      columns: [
        {
          title: this.$t('商品编码'),
          align: 'center',
          dataIndex: 'goodsCode',
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsName',
          customRender: (t, r) => {
            if (t == '合计') {
              return this.$t(t)
            }
            return t
          },
        },
        {
          title: this.$t('商品分类'),
          align: 'center',
          dataIndex: 'goodsKindName',
        }
      ],
      dataSource: [],
      queryParam: {
        parentKindId: '',
      },
      url: {
        list: '/member/bisGiftReclaimRecord/getReport',
        exportXlsUrl: '/member/bisGiftReclaimRecord/exportReportXls',
      },
      treeData: [],
      allChildKind: [],
      isCustom: true,
    }
  },
  mounted() {
    this.select()
  },
  methods: {
    customRow(r, i) {
      return {
        on:{
          click: () => {
            let x = this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i})
            if( x == -1 ){
              this.selectedRowIndex.push('a'+this.ipagination.current+''+i)
            }else{
              this.selectedRowIndex.splice(x, 1)
            }
          }
        },
        style: {
          whiteSpace: 'break-spaces',
          wordBreak: 'break-word',
          overflowWrap: 'break-word',
          backgroundColor: this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i}) > -1 ? '#FFECE6' : '',
          fontWeight: r.goodsName == '合计' ? 700 : 400,
        },
      }
    },
    initDictConfig() {
      if (localStorage.getItem('operateCoin') == 1) {
        this.columns.splice(this.columns.length, 0, {
          title: this.$t('回收币'),
          align: 'center',
          children: [
            {
              title: this.$t('数量'),
              align: 'center',
              dataIndex: 'coinNumber',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
            {
              title: this.$t('币值'),
              align: 'center',
              dataIndex: 'coin',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(2);
              }
            },
          ],
        })
      }
      if (localStorage.getItem('operateLottery') == 1) {
        this.columns.splice(this.columns.length, 0, {
          title: this.$t('回收彩票'),
          align: 'center',
          children: [
            {
              title: this.$t('数量'),
              align: 'center',
              dataIndex: 'lotteryNumber',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
            {
              title: this.$t('彩票'),
              align: 'center',
              dataIndex: 'lottery',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
          ],
        })
      }
      if (localStorage.getItem('operateCredit') == 1) {
        this.columns.splice(this.columns.length, 0, {
          title: this.$t('回收积分'),
          align: 'center',
          children: [
            {
              title: this.$t('数量'),
              align: 'center',
              dataIndex: 'integralNumber',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
            {
              title: this.$t('积分'),
              align: 'center',
              dataIndex: 'integral',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
          ],
        })
      }
      if (localStorage.getItem('operateALottery') == 1) {
        this.columns.splice(this.columns.length, 0, {
          title: this.$t('回收A票'),
          align: 'center',
          children: [
            {
              title: this.$t('数量'),
              align: 'center',
              dataIndex: 'alotteryNumber',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
            {
              title: this.$t('A票'),
              align: 'center',
              dataIndex: 'alottery',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
          ],
        })
      }
      if (localStorage.getItem('operateACoin') == 1) {
        this.columns.splice(this.columns.length, 0, {
          title: this.$t('回收A币'),
          align: 'center',
          children: [
            {
              title: this.$t('数量'),
              align: 'center',
              dataIndex: 'acoinNumber',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
            {
              title: this.$t('A币'),
              align: 'center',
              dataIndex: 'acoin',
              customCell: (r, i) => {
                return {
                  style: {
                    textAlign: 'right',
                  },
                }
              },
              customRender: (text, r) => {
                text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
                return text.toFixed(0);
              }
            },
          ],
        })
      }
      this.searchQuery()
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
        if (res.success) {
          that.allChildKind = []
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
            that.treeData.unshift({ key: '', id: '', value: '', title: that.$t('全部') })
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, that.$t('提示'), {
            confirmButtonText: that.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      getAction('/goods/bisGoodsKind/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      let that = this
      return result.map((item) => {
        if (item.hasChild == 0 && item.key != '0') {
          that.allChildKind.push(item.key)
        }
        return item
      })
    },
    searchReset() {
      this.queryParam = {
        parentKindId: '',
      }
      this.searchQuery()
    },
    handleDateClick(evt) {
      if (evt.key == '1') {
        this.queryParam.createTime_begin = moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '2') {
        this.queryParam.createTime_begin = moment().subtract(2, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '3') {
        this.queryParam.createTime_begin = moment().subtract(6, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '4') {
        this.queryParam.createTime_begin = moment().subtract(1, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '5') {
        this.queryParam.createTime_begin = moment().subtract(3, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '6') {
        this.queryParam.createTime_begin = moment().subtract(6, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '7') {
        this.queryParam.createTime_begin = moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.createTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>